<?php
namespace Mobile\Controller;

use Common\Controller\MHomeBaseController;

/**
 * 登陆控制器
 */
class LoginController extends MHomeBaseController
{
    /**
     *
     */
    public function index()
    {

        //session('member_id',NULL);
        $member_id = session('member_id');
        //print_r($_COOKIE);exit;
        if (!empty($member_id)) {
//已经登录
            redirect(U("Mobile/Index/index"));
        } else {
            $this->display();
        }
    }
    public function agreement()
    {

        $agreement = $_SESSION['agreement'];
        if (empty($agreement)) {
            redirect(U("Mobile/Member/index"));

        } else {

            if ($_POST) {
                $data     = M('member')->where('member_id=' . $agreement)->find();
                $_SESSION = array(
                    'member_id'       => $data['member_id'],
                    'member_name'     => $data['member_name'],
                    'last_login_time' => $data['last_login_time'],
                );
                $updata_data['last_login_ip'] = get_client_ip(0, true);

                $v = get_rand_number(1, 10, 6);
                $v = implode('', $v);

                $_SESSION['verinfo'] = $v;

                $updata_data['last_login_time'] = time();
                $updata_data['verinfo']         = $v;

                $condition['member_id'] = $data['member_id'];

                if ($remember) {
                    cookie("member_name", $data['member_name'], 3600 * 24 * 30);
                    cookie("member_pwd", I('post.password'), 3600 * 24 * 30);
                }

                $result = M('member')->where($condition)->save($updata_data);
                die(json_encode(array('result' => 1, 'message' => '登录成功、前往管理后台')));
                //redirect(U("Home/Member/index"));
            }
            $this->display('member-agreement');

        }
    }

    public function logout()
    {
        session('member_id', null);
        session('member_name', null);
        redirect('/');
    }

    public function dologin()
    {

        // 做一个简单的登录 组合where数组条件
        $map['member_name']     = I('post.name');
        $map['member_password'] = md5(I('post.pwd'));
        $pan                    = I('post.pan');
        $data                   = M('member')->where($map)->find();
        //print_r($data);exit;
        if (empty($data)) {
            //$this->error('账号或密码错误');

            die(json_encode(array('result' => 0, 'message' => '账号或密码错误')));
        } else {
            if ($data['status'] == 0) {
                die(json_encode(array('result' => 0, 'message' => '您的账号被禁用')));
            }
            $_SESSION['agreement'] = $data['member_id'];
            $update_array['pan']   = $pan;

            M('member')->where('member_id=' . $data['member_id'])->save($update_array);

            die(json_encode(array('result' => 1, 'message' => '登录成功、前往管理后台')));
            // $this->success('登录成功、前往管理后台',U('Home/Member/index'));
        }
    }

}
